% Copyright (C) 2014-2022 Benjamin Born and Johannes Pfeifer 
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

%% Notes:
% Requires running table_2.m with 
%   unconditional_model_dummy = 1;
%   sign_dummy = 0;
% for the three options 
%   impulse_name = 'g_growth_fe'; % OE shocks
%   impulse_name = 'OECD_Forecast_Error_g'; % OECD shocks
%   impulse_name = 'BP_shock_base_spread_control'; % VAR shocks (max sample)
% to generate the
% mat-files loaded in this file

fid_g=fopen(['Results/Descriptives_table.txt'],'w+');
topstring_descriptives={'\begin{table}[t!]'
'  \centering'
'  \caption{Sample ranges}'
'   \tabcolsep=.2in'
'  \scalebox{0.9}{'
'    \begin{tabular}{lcccc}'
'    \toprule'
'    & \multicolumn{2}{c}{EMU} & \multicolumn{2}{c}{Float}\\'
'    \midrule'
'    Country   & Range & T & Range & T \\'
'    \midrule'};
for ii=1:size(topstring_descriptives,1)
fprintf(fid_g,'%-150s \n',topstring_descriptives{ii,1});
end

Data.EMU=load('Results/Descriptives_PF_shock_G');
Data.FLOAT=load('Results/Descriptives_BP_shock_base');

identification_strings={'EMU';'FLOAT'};
country_names=union(Data.EMU.country_info_cell(:,1),Data.FLOAT.country_info_cell(:,1));

sum_obs=zeros(size(identification_strings,1),1);
for country_iter=1:size(country_names,1)
    country_info{country_iter,1}=country_names{country_iter};
    for identification_iter=1:size(identification_strings,1)
        country_pos=strmatch(country_names{country_iter},Data.(identification_strings{identification_iter}).country_info_cell(:,1),'exact');
        if ~isempty(country_pos)
            country_info{country_iter,1+2*(identification_iter-1)+1}=[Data.(identification_strings{identification_iter}).country_info_cell{country_pos,2}(1:end) '-' Data.(identification_strings{identification_iter}).country_info_cell{country_pos,3}(3:end)];
            country_info{country_iter,1+2*(identification_iter-1)+2}=num2str(Data.(identification_strings{identification_iter}).country_info_cell{country_pos,4});
            sum_obs(identification_iter,1)=sum_obs(identification_iter,1)+Data.(identification_strings{identification_iter}).country_info_cell{country_pos,4};
        else
            country_info{country_iter,1+2*(identification_iter-1)+1}='-';
            country_info{country_iter,1+2*(identification_iter-1)+2}='-';
        end
    end
        fprintf(fid_g,'%-30s \t & \t %15s \t & \t %6s \t & \t %15s \t & \t %6s\\\\ \n',...
    country_info{country_iter,:});
end
fprintf(fid_g,'%-30s \t & \t %15s \t & \t %6s \t & \t %15s \t & \t %6s \t \\\\ \n',...
    'Total','',num2str(sum_obs(1)),'',num2str(sum_obs(2)));



bottomstring_descriptives={'    \bottomrule'
'    \end{tabular}'
' }'
'\caption*{\footnotesize{\emph{Notes:} Range refers to the first and last observation available. Limiting factors for the two samples are the availability'
'of government spending growth forecasts and sovereign bond spreads. $T$ refers to the number of observations used'
'for the particular country after accounting for missing values and lag construction.}}'
'  \label{tab:sample}'
'\end{table}'};

for ii=1:size(bottomstring_descriptives,1)
fprintf(fid_g,'%-150s \n',bottomstring_descriptives{ii,1});
end

fclose(fid_g)
